package com.utils
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class Print extends Sprite
	{
		private static var instance : Print;

		private var container : Stage;

		/**
		 * 一般信息颜色
		 */
		public static const NORMAL_INFOR_COLOR : int = 0xFFFFFF;

		/**
		 * 错误信息颜色
		 */
		public static const ERROR_INFOR_COLOR : int = 0xFF0000;

		/**
		 * 警告颜色
		 */
		public static const WARN_INFOR_COLOR : int = 0xFFFF00;

		/**
		 * SOCKET信息颜色
		 */
		public static const SOCKET_INFOR_COLOR : int = 0xFFFF00;

		/**
		 * 可以显示的行数
		 */
		private var max_line_count : int = 0;

		/**
		 * 背景颜色
		 */
		private var backgroundColor : int = 0x000000;

		/**
		 * 背景透明度
		 */
		private var backgroundAlpha : Number = 0.1;

		/**
		 * 显示的文本
		 */
		private var printField : TextField;

		private var showInfo : String = "";

		/**
		 * 自动滚动
		 */
		private var isAutoScorll : Boolean = true;

		/**
		 * 描黑滤镜
		 */
		private var blackFilters : Array = [new GlowFilter(0x000000, 1, 2, 2, 255)];

		public static function init(container : Stage) : void
		{
			instance = new Print(container);
			instance.init();
		}

		public function Print(container : Stage)
		{
			super();
			this.container = container;
		}

		/**
		 * 初始化
		 *
		 */
		private function init() : void
		{
			if (instance == null)
			{
				trace("没有初始化");
				return;
			}
			this.graphics.beginFill(backgroundColor, backgroundAlpha);
			this.graphics.drawRect(0, 0, container.stageWidth, container.stageHeight);
			this.graphics.endFill();

			this.mouseEnabled = false;
			this.mouseChildren = false;

			printField = new TextField();
			printField.width = container.stageWidth;
			printField.height = container.stageHeight;
			printField.selectable = false;
			printField.wordWrap = true;
			printField.text = "";
			printField.filters = blackFilters;

			var format : TextFormat = new TextFormat();
			format.size = 13;
			format.font = "宋体";
			printField.defaultTextFormat = format;

			addChild(printField);

			container.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
		}

		private function onKeyUpHandler(evt : KeyboardEvent) : void
		{
			if (evt.ctrlKey && evt.altKey && evt.keyCode == Keycode.O)
			{
				if (parent)
					showPrint(true);
				else
					showPrint(false);
			}

			if (!isShow)
				return;

			switch (evt.keyCode)
			{
				//向下翻页
				case Keycode.PageDown:
					if (printField.scrollV + max_line_count > printField.numLines)
					{
						isAutoScorll = true;
						printField.scrollV = printField.numLines - max_line_count + 1;
					}
					else
						printField.scrollV = printField.scrollV + max_line_count;
					break;
				//向上翻页
				case Keycode.PageUp:
					isAutoScorll = false;
					if (printField.scrollV - max_line_count <= 0)
						printField.scrollV = 1;
					else
						printField.scrollV = printField.scrollV - max_line_count;
					break;
				//隐藏界面
				case Keycode.Esc:
					showPrint(false);
					break;
				//清除
				case Keycode.Delete:
					printField.text = "";
					break;
			}
		}

		private function get isShow() : Boolean
		{
			return this.parent != null;
		}

		/**
		 * 是否显示界面
		 * @param show
		 *
		 */
		private function showPrint(show : Boolean) : void
		{
			if (!show && parent)
				parent.removeChild(this);
			else
				container.addChild(this);
		}

		/**
		 * 错误信息打印
		 * @param	...arg
		 */
		public static function error(... arg) : void
		{
			write(arg, ERROR_INFOR_COLOR);
			trace("错误信息: ", arg);
		}

		/**
		 * 警告
		 * @param arg
		 *
		 */
		public static function warning(... arg) : void
		{
			write(arg, WARN_INFOR_COLOR);
		}

		/**
		 * 普通打印
		 * @param	...arg
		 */
		public static function print(... arg) : void
		{
			write(arg, NORMAL_INFOR_COLOR);
		}

		public static function socket(... arg) : void
		{
			write(arg, SOCKET_INFOR_COLOR);
		}

		private static function write(inforList : Array, color : int) : void
		{
			var infor : String = inforList.join(" , ");
			instance.write("<font color='#" + color.toString(16) + "'>\\&gt;" + infor + "</font>");
		}

		public function write(infor : String) : void
		{
			if (instance == null)
			{
				return;
			}
			showInfo += infor + "\n";
			printField.htmlText = showInfo;

			if (isAutoScorll)
				printField.scrollV = printField.numLines + 1;

			//记录当前可打印的行数
			if (max_line_count == 0 && printField.scrollV != 1)
			{
				max_line_count = printField.numLines - 1;
			}
		}
	}
}